package com.face.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.face.common.pojo.DataGridResult;
import com.face.mapper.TbCheckMapper;
import com.face.mapper.TbStudentMapper;
import com.face.pojo.Query;
import com.face.pojo.QueryResult;
import com.face.pojo.TbCheck;
import com.face.pojo.TbCheckExample;
import com.face.pojo.TbCheckExample.Criteria;
import com.face.pojo.TbStudent;
import com.face.service.CheckService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CheckServiceImpl implements CheckService {

	@Autowired
	private TbCheckMapper checkMapper;

	@Autowired
	private TbStudentMapper studentMapper;

	@Override
	public DataGridResult list(String type,Integer page,Integer rows) {
		try {
			TbCheckExample example = new TbCheckExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdIsNotNull();
			PageHelper.startPage(page, rows);
			List<TbCheck> list = checkMapper.selectByExample(example);
			List<QueryResult> result = new ArrayList<>();

			for (TbCheck check : list) {
				QueryResult q = new QueryResult();
				TbStudent student = studentMapper.selectByPrimaryKey(check.getStudentId());
				q.setId(check.getId());
				q.setStudentName(student.getStudentName());
				q.setCollegeName(student.getCollegeName());
				q.setMajorName(student.getMajorName());
				q.setClassName(student.getClassName());
				q.setCheckType(check.getCheckType());
				q.setCheckAddress(check.getCheckAddress());
				q.setCheckTime(check.getCheckTime());
				q.setCheckYear(check.getCheckYear());
				result.add(q);
			}
			DataGridResult gridResult=new DataGridResult();
			gridResult.setRows(result);
			PageInfo<TbCheck> info=new PageInfo<>(list);
			gridResult.setTotal(info.getTotal());
			return gridResult;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public DataGridResult getCheckByCollege(Integer page, Integer rows, String collegeName) {
		try {
			TbCheckExample example = new TbCheckExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdIsNotNull();
			PageHelper.startPage(page, rows);
			List<TbCheck> list = checkMapper.selectByExample(example);
			List<QueryResult> result = new ArrayList<>();
			for(TbCheck check:list){
				QueryResult q = new QueryResult();
				TbStudent student = studentMapper.selectByPrimaryKey(check.getStudentId());
				if(student.getCollegeName().equals(collegeName)){
					q.setId(check.getId());
					q.setStudentName(student.getStudentName());
					q.setCollegeName(student.getCollegeName());
					q.setMajorName(student.getMajorName());
					q.setClassName(student.getClassName());
					q.setCheckType(check.getCheckType());
					q.setCheckAddress(check.getCheckAddress());
					q.setCheckTime(check.getCheckTime());
					q.setCheckYear(check.getCheckYear());
					result.add(q);
				}
			}
			DataGridResult gridResult=new DataGridResult();
			gridResult.setRows(result);
			PageInfo<TbCheck> info=new PageInfo<>(list);
			gridResult.setTotal(info.getTotal());
			return gridResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public DataGridResult getCheckByMajor(Integer page, Integer rows, String majorName) {
		try {
			TbCheckExample example = new TbCheckExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdIsNotNull();
			PageHelper.startPage(page, rows);
			List<TbCheck> list = checkMapper.selectByExample(example);
			List<QueryResult> result = new ArrayList<>();
			for(TbCheck check:list){
				QueryResult q = new QueryResult();
				TbStudent student = studentMapper.selectByPrimaryKey(check.getStudentId());
				if(student.getMajorName().equals(majorName)){
					q.setId(check.getId());
					q.setStudentName(student.getStudentName());
					q.setCollegeName(student.getCollegeName());
					q.setMajorName(student.getMajorName());
					q.setClassName(student.getClassName());
					q.setCheckType(check.getCheckType());
					q.setCheckAddress(check.getCheckAddress());
					q.setCheckTime(check.getCheckTime());
					q.setCheckYear(check.getCheckYear());
					result.add(q);
				}
			}
			DataGridResult gridResult=new DataGridResult();
			gridResult.setRows(result);
			PageInfo<TbCheck> info=new PageInfo<>(list);
			gridResult.setTotal(info.getTotal());
			return gridResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public DataGridResult getCheckByClass(Integer page, Integer rows, String className,String majorName) {
		try {
			TbCheckExample example = new TbCheckExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdIsNotNull();
			PageHelper.startPage(page, rows);
			List<TbCheck> list = checkMapper.selectByExample(example);
			List<QueryResult> result = new ArrayList<>();
			for(TbCheck check:list){
				QueryResult q = new QueryResult();
				TbStudent student = studentMapper.selectByPrimaryKey(check.getStudentId());
				if(student.getMajorName().equals(majorName)&&student.getClassName().equals(className)){
					q.setId(check.getId());
					q.setStudentName(student.getStudentName());
					q.setCollegeName(student.getCollegeName());
					q.setMajorName(student.getMajorName());
					q.setClassName(student.getClassName());
					q.setCheckType(check.getCheckType());
					q.setCheckAddress(check.getCheckAddress());
					q.setCheckTime(check.getCheckTime());
					q.setCheckYear(check.getCheckYear());
					result.add(q);
				}
			}
			DataGridResult gridResult=new DataGridResult();
			gridResult.setRows(result);
			PageInfo<TbCheck> info=new PageInfo<>(list);
			gridResult.setTotal(info.getTotal());
			return gridResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public DataGridResult getCheckByYear(Integer page, Integer rows, String year) {
		try {
			TbCheckExample example = new TbCheckExample();
			Criteria criteria = example.createCriteria();
			criteria.andCheckYearEqualTo(Integer.parseInt(year));
			PageHelper.startPage(page, rows);
			List<TbCheck> list = checkMapper.selectByExample(example);
			List<QueryResult> result = new ArrayList<>();
			for(TbCheck check:list){
				QueryResult q = new QueryResult();
				TbStudent student = studentMapper.selectByPrimaryKey(check.getStudentId());
					q.setId(check.getId());
					q.setStudentName(student.getStudentName());
					q.setCollegeName(student.getCollegeName());
					q.setMajorName(student.getMajorName());
					q.setClassName(student.getClassName());
					q.setCheckType(check.getCheckType());
					q.setCheckAddress(check.getCheckAddress());
					q.setCheckTime(check.getCheckTime());
					q.setCheckYear(check.getCheckYear());
					result.add(q);
				
			}
			DataGridResult gridResult=new DataGridResult();
			gridResult.setRows(result);
			PageInfo<TbCheck> info=new PageInfo<>(list);
			gridResult.setTotal(info.getTotal());
			return gridResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
